﻿<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="报检单号">
                            <el-input v-model="query.code" placeholder="报检单号" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="报检类型">
                            <el-select v-model='query.type' placeholder='请选择' @keyup.enter.native="search" clearable
                                :style='{ "width": "100%" }'>
                                <el-option v-for="(item, index) in ipqcInspectionTypeData" :key="index"
                                    :label="item.fullName" :value="item.enCode"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="物料">
                            <el-input v-model="query.materialCode" placeholder="物料" @keyup.enter.native="search"
                                clearable />
                        </el-form-item>
                    </el-col>
                    <template v-if="showAll">
                        <el-col :span="6">
                            <el-form-item label="生产订单">
                                <el-input v-model="query.orderNo" placeholder="生产订单号" @keyup.enter.native="search"
                                    clearable />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="检验员">
                                <el-input v-model="query.employeeCode" placeholder="检验员" @keyup.enter.native="search"
                                    clearable />
                                <!-- <user-select v-model="query.employeeCode" @keyup.enter.native="search" placeholder="检验员" /> -->
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="状态">
                                <el-select v-model='query.status' placeholder='状态' clearable>
                                    <el-option v-for="(item, index) in statusData" :key="index" :label="item.fullName"
                                        :value="item.enCode"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                            <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true"
                                v-if="!showAll">展开</el-button>
                            <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                        <el-button type="primary" icon="el-icon-plus" @click="addHandle()">新增</el-button>
                        <el-button :loading="btnLoading" type="primary" icon="el-icon-check"
                            @click="handleConfirm()">确认</el-button>
                        <el-button type="text" icon="el-icon-download" @click="exportData()">导出</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()">批量删除</el-button>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :element-loading-text="elementLoadingText" :selectable="selectHandle"
                    :data="list" has-c @selection-change='handleSelectionChange' custom-column>
                    <el-table-column prop="overdue" label="标记" width="70" align="center">
                        <template slot="header" slot-scope="scope">
                            <el-dropdown>
                                <span class="el-dropdown-link">标记 <i class="el-icon-question"></i>
                                </span>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="right"><i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#ff625c;font-size:14px;">逾期 </i></el-dropdown-item>
                                    <el-dropdown-item command="bottom"><i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#f9a646;font-size:14px;">即将逾期 </i></el-dropdown-item>
                                    <el-dropdown-item command="refresh"><i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#6dcc51;font-size:14px;">正常</i></el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </template>
                        <template slot-scope="scope">
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#ff625c;font-size:16px;"
                                v-if="scope.row.overdue == 2"></i>
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#f9a646;font-size:16px;"
                                v-else-if="scope.row.overdue == 1"></i>
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#6dcc51;font-size:16px;"
                                v-else></i>
                        </template>
                    </el-table-column>
                    <el-table-column label="报检单号" prop="code" width="150" align="left">
                    </el-table-column>
                    <el-table-column label="报检类型" prop="type" width="100" align="left">
                        <template slot-scope="scope">
                            制程检验-{{ getIpqcInspectTypeDes(scope.row.type) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="工厂信息" prop="workshopId" width="150" :show-overflow-tooltip="true" align="left">
                        <template slot-scope="scope">
                            <template v-if="scope.row.workshopId">
                                {{ scope.row.workshopId }}
                            </template>
                            <template v-if="scope.row.teamId">
                                {{ scope.row.teamId }}
                            </template>
                            <template v-if="scope.row.workshopSectionId">
                                {{ scope.row.workshopSectionId }}
                            </template>
                            <template v-if="scope.row.stationId">
                                {{ scope.row.stationId }}
                            </template>

                        </template>
                    </el-table-column>

                    <el-table-column label="物料名称" prop="materialCode" width="150" :show-overflow-tooltip="true"
                        align="left">
                    </el-table-column>
                    <el-table-column label="⼯序" prop="processIdList" width="150" :show-overflow-tooltip="true" align="left">
                    </el-table-column>
                    <el-table-column label="检验⽅案" prop="inspectionCase" width="150" :show-overflow-tooltip="true"
                        align="left">
                    </el-table-column>
                    <el-table-column label="检验员" prop="employeeName" width="100" align="left">
                    </el-table-column>
                    <el-table-column label="⽣产订单号" prop="orderNo" width="150" align="left">
                    </el-table-column>
                    <el-table-column label="⽣产数量" prop="productionQty" width="150" align="left">
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="150" align="left">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status == 0" type="info">未确认</el-tag>
                            <el-tag v-if="scope.row.status == 1">执行中</el-tag>
                            <el-tag v-if="scope.row.status == 2" type="success">已确认</el-tag>
                            <el-tag v-if="scope.row.status == 3" type="warning">已关闭</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="报检日期" prop="date" width="150" align="left">
                        <template slot-scope="scope">{{ scope.row.date | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="按天生成" prop="dayFlag" width="100" align="left">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.dayFlag == 'N'" type="warning">关闭</el-tag>
                            <el-tag v-if="scope.row.dayFlag == 'Y'" type="success">开启</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="天" prop="dayValue" width="50" align="left">
                    </el-table-column>
                    <el-table-column label="按小时生成" prop="hourFlag" width="100" align="left">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.hourFlag == 'N'" type="warning">关闭</el-tag>
                            <el-tag v-if="scope.row.hourFlag == 'Y'" type="success">开启</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="小时" prop="hourValue" width="50" align="left">
                    </el-table-column>
                    <el-table-column label="⾃动关闭" prop="closeFlag" width="100" align="left">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.closeFlag == 'N'" type="warning">关闭</el-tag>
                            <el-tag v-if="scope.row.closeFlag == 'Y'" type="success">开启</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" width="150" :show-overflow-tooltip="true" align="left">
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" @click="updateHandle(scope.row.id)"
                                :disabled="scope.row.status != 0">编辑</el-button>
                            <el-button type="text" @click="handleDel(scope.row.id)" class='SHZY-table-delBtn'
                                :disabled="scope.row.status != 0">删除</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh('edit', arguments[0])" />
        <SHZY-Add-Form v-if="addFormVisible" ref="SHZYAddForm" @refresh="refresh('add', arguments[0])" />
        <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    </div>
</template>
<script>
import { getIpqcInspectionList, delIpqcInspectionBatch, delIpqcInspection, exportData, confirmIpqcInspection } from '@/api/q1/business/ipqcInspection'
import SHZYForm from './Form'
import SHZYAddForm from './Add'
import ExportBox from './ExportBox'
export default {
    name: "business-ipqcInspection",
    components: { SHZYForm, SHZYAddForm, ExportBox },
    data() {
        return {
            showAll: false,
            btnLoading: false,
            ipqcInspectionTypeData: [],
            query: {
                code: undefined,
                type: undefined,
                materialCode: undefined,
                orderNo: undefined,
                employeeCode: undefined,
                status: undefined,
            },
            list: [],
            listLoading: true,
            elementLoadingText: "",
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            formVisible: false,
            addFormVisible: false,
            exportBoxVisible: false,
            columnList: [
                { prop: 'code', label: '报检单号' },
                { prop: 'date', label: '报检日期' },
                { prop: 'type', label: '报检类型' },
                { prop: 'workshopId', label: '⻋间' },
                { prop: 'teamId', label: '班组' },
                { prop: 'workshopSectionId', label: '⼯段' },
                { prop: 'stationId', label: '⼯位' },
                { prop: 'materialCode', label: '物料名称' },
                { prop: 'processIdList', label: '⼯序' },
                { prop: 'inspectionCase', label: '检验⽅案Guid' },
                { prop: 'productionQty', label: '⽣产数量' },
                { prop: 'employeeCode', label: '检验员' },
                { prop: 'employeeName', label: '检验员名称' },
                { prop: 'orderNo', label: '⽣产订单号' },
                { prop: 'remark', label: '备注' },
                { prop: 'status', label: '状态' },
                { prop: 'overdue', label: '逾期状态' },
                { prop: 'closeFlag', label: '⾃动关闭' },
                { prop: 'dayFlag', label: '天标识' },
                { prop: 'dayValue', label: '天值' },
                { prop: 'hourFlag', label: '小时标识' },
                { prop: 'hourValue', label: '小时值' },
            ],
            statusData: [
                { 'fullName': "未确认", 'enCode': "0" },
                { 'fullName': "执行中", 'enCode': "1" },
                { 'fullName': "已确认", 'enCode': "2" },
                { 'fullName': "已关闭", 'enCode': "3" }
            ]
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.getDictionaryData()
        this.initData()
    },
    methods: {
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'ipqcInspectionType' }).then((res) => {
                this.ipqcInspectionTypeData = res
            })
        },
        getIpqcInspectTypeDes(enCode) {
            let ipqcInspectionTypeInfo = this.ipqcInspectionTypeData.find(x => x.enCode == enCode)
            if (ipqcInspectionTypeInfo) {
                return ipqcInspectionTypeInfo.fullName
            } else {
                return enCode
            }
        },
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }
            getIpqcInspectionList(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            })
        },
        selectHandle(row) {
            if (row.status != 0) {
                return false
            }
            return true
        },
        handleDel(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                delIpqcInspection(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => {
            });
        },
        handleSelectionChange(val) {
            const res = val.map(item => item.id)
            this.multipleSelection = res
        },
        handleBatchRemoveDel() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return
            }
            const ids = this.multipleSelection
            this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {
                delIpqcInspectionBatch(ids).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => { })
        },
        handleConfirm() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return
            }
            const ids = this.multipleSelection
            this.$confirm('您确定要确认这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {
                this.elementLoadingText = "处理确认报检单中";
                this.listLoading = true;
                this.btnLoading = true
                confirmIpqcInspection(ids).then(res => {
                    this.btnLoading = false
                    this.elementLoadingText = "";
                    this.listLoading = false;
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                }).catch((res) => {
                    this.btnLoading = false
                    this.elementLoadingText = "";
                    this.listLoading = false;
                })
            }).catch(() => { })
        },
        addHandle() {
            this.addFormVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYAddForm.init()
            })
        },
        updateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYForm.init(id)
            })
        },
        exportData() {
            this.exportBoxVisible = true
            this.$nextTick(() => {
                this.$refs.ExportBox.init(this.columnList)
            })
        },
        download(data) {
            let query = { ...data, ...this.listQuery, ...this.query, menuId: this.menuId }
            exportData(query).then(res => {
                if (!res.data.url) return
                this.shzy.downloadFile(res.data.url)
                this.$refs.ExportBox.visible = false
                this.exportBoxVisible = false
            })
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        refresh(type, isrRefresh) {
            if (type == "add") {
                this.addFormVisible = false
            } else {
                this.formVisible = false
            }
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        }
    }
}
</script>